#include "GameRunning.h"

GameRunning::GameRunning(GameState &_state):
GameRoutine(_state)
{
	customModel = (CustomModel *) model;
}

GameRunning::~GameRunning(void)
{
}

void GameRunning::run()
{
	b2Vec2 sVector(4, 4);

	handleKeys();

	controller->updateGameObjects();
	controller->updatePhysicObjects();
	controller->manager.update();

	buffer.str("");
	buffer << "Player 1: " << customModel->carA->laps << " laps";
	customModel->p1Laps->setText(buffer.str());

	buffer.str("");
	buffer << "Player 2: " << customModel->carB->laps << " laps";
	customModel->p2Laps->setText(buffer.str());

	customModel->dManager.update();

	//view->sendOutput();
	view->getInput();
}

void GameRunning::pauseGame()
{
	customModel->pauseAnim->show();
	controller->swapState("paused");
}

void GameRunning::handleKeys()
{
	if( view->keyInput.keyDown(KEY_ESC) )
		controller->running = false;

	if( view->keyInput.keyDown(KEY_DOWN) )
		customModel->carA->slowDown();
	if( view->keyInput.keyDown(KEY_UP) )
		customModel->carA->accelerate();
	if( view->keyInput.keyDown(KEY_LEFT) )
		customModel->carA->stearLeft();
	if( view->keyInput.keyDown(KEY_RIGHT) )
		customModel->carA->stearRight();
	if( view->keyInput.keyDown(KEY_RSHIFT) )
		customModel->carA->fireCannon();
	if( view->keyInput.keyDown(KEY_RCONTROL) )
		customModel->carA->placeMine();

	if( view->keyInput.keyDown(KEY_S) )
		customModel->carB->slowDown();
	if( view->keyInput.keyDown(KEY_W) )
		customModel->carB->accelerate();
	if( view->keyInput.keyDown(KEY_A) )
		customModel->carB->stearLeft();
	if( view->keyInput.keyDown(KEY_D) )
		customModel->carB->stearRight();
	if( view->keyInput.keyDown(KEY_F) )
		customModel->carB->fireCannon();
	if( view->keyInput.keyDown(KEY_G) )
		customModel->carB->placeMine();


	if( view->keyInput.keyPressed(KEY_P) )
		pauseGame();
}
